package cn.pn.project.service;

import cn.pn.project.common.RedisUtils;
import cn.pn.project.mapper.schedule.SeatDao;
import cn.pn.project.pojo.schedule.Seat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.List;

@RestController
public class SeatService {

    @Autowired
    private SeatDao seatDao;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据场地id查询座位信息
     * @param placeId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/getSeatByPlaceId",method = RequestMethod.POST)
    public Object getSeatByPlaceId(@RequestParam("placeId") Integer placeId) throws SQLException {
        String key="placeId"+placeId;
        if(redisUtils.exist(key)){
            System.out.println("============================从redis中取值");
            return redisUtils.get(key);
        }else{
            if(redisUtils.set(key,seatDao.getSeatByPlaceId(placeId))){
                System.out.println("==============================添加数据到redis中");
            }
            return seatDao.getSeatByPlaceId(placeId);
        }
    }


    /**
     * 根据座位id查询座位信息，多个座位id用，隔开
     * @param seatId
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "getAllSeatInfo",method = RequestMethod.POST)
    public List<Seat> getAllSeatInfo(@RequestParam("seatId") String seatId) throws SQLException{
        return seatDao.getAllSeatInfo(seatId);
    }

}
